require 'fox16'
require 'active_record'
ActiveRecord::Base.establish_connection(
      :adapter => "sqlite3" ,
      :database => "db/development.sqlite3"
)
include Fox

class Gsclass < ActiveRecord::Base
end
class Character < ActiveRecord::Base
end
class Classkey < ActiveRecord::Base
end

class CharacterKeyGen < FXMainWindow
  attr_accessor(:earth, :fire, :wind, :water)
  
  def initialize(app)
    super(app, "Character Key Tester", :width => 400, :height => 400)
    charid=0,
    key=0
    @splitter = FXSplitter.new(self, :opts => LAYOUT_FILL)
    @splitter.splitterStyle|=SPLITTER_VERTICAL
    hmainframe=FXHorizontalFrame.new(@splitter,:opts=>LAYOUT_FILL)
    @text= FXTextField.new(@splitter, 20, :opts => TEXTFIELD_NORMAL|LAYOUT_SIDE_BOTTOM|LAYOUT_FILL_X)
    vchild=FXVerticalFrame.new(hmainframe,:opts=>LAYOUT_FILL)
    charCombo=FXListBox.new(vchild,:opts => LISTBOX_NORMAL|FRAME_SUNKEN|LAYOUT_FILL_X)
    charCombo.numVisible = 7
    @chars=Character.find(:all)
    for x in @chars
      charCombo.appendItem(x.name.to_s)
    end
    charCombo.connect(SEL_COMMAND) do |sender,sel,data|
      puts "Charater:",data,"\n"
      charid=data
      
    end
    earthList=FXList.new(hmainframe,:opts => LIST_SINGLESELECT|FRAME_THICK)
    fireList=FXList.new(hmainframe,:opts => LIST_SINGLESELECT|FRAME_SUNKEN)
    windList=FXList.new(hmainframe,:opts => LIST_SINGLESELECT|FRAME_SUNKEN)
    waterList=FXList.new(hmainframe,:opts => LIST_SINGLESELECT|FRAME_SUNKEN)
    0.upto(7) { |x| earthList.appendItem(x.to_s) }
    0.upto(7) { |x| fireList.appendItem(x.to_s) }
    0.upto(7) { |x| windList.appendItem(x.to_s) }
    0.upto(7) { |x| waterList.appendItem(x.to_s) }
    earthList.numVisible = 8
    fireList.numVisible = 8
    windList.numVisible = 8
    waterList.numVisible = 8
    #classkey=(charid*8**4)+(e.to_i*8**3)+(f.to_i*8**2)+(wi.to_i*8)+(wa.to_i)
    earthList.selectItem(0)
    fireList.selectItem(0)
    windList.selectItem(0)
    waterList.selectItem(0)
        @earth=0
              @fire=0
              @wind=0
              @water=0
    addKeys=FXButton.new(hmainframe,"Next",:opts=> BUTTON_NORMAL|LAYOUT_SIDE_BOTTOM)
    addKeys.connect(SEL_COMMAND) do
            #if e+f+wi+wa<=7
            #  classkey=(charid*8**4)+(e*8**3)+(f*8**2)+(wi*8)+(wa)
            #  puts "Character: ",Character.find(charid+1).name
            #  puts "E:",e,"F:",f,"WI:",wi,"WA:",wa,"KEY:",classkey,"Class:",Gsclass.find(Classkey.find_by_key(classkey).gsclass_id).name
            #  puts Classkey.find_by_key(classkey).gsclass_id
      
   
    count8(charid)
    earthList.selectItem(@earth)
    fireList.selectItem(@fire)
    windList.selectItem(@wind)
    waterList.selectItem(@water)

    
    
    end
     self.connect(SEL_CLOSE) do
      app.reg.writeIntEntry("Settings", "splitSize", @splitter.getSplit(0))
      app.exit(0)

    end
  end
  def count8 (charid)
    #Base 8 counting where the sum of the digits is less than 8
    if @water<7
        @water=@water+1
      else
        @water=0
        if @wind<7
          @wind=@wind+1
        else
          @wind=0
          if @fire<7
            @fire=@fire+1
          else
            @fire=0
            if @earth<7
              @earth=@earth+1
            else
              @earth=0
              @fire=0
              @wind=0
              @water=0
            end
          end
      end
    end
    #output fo debug
    if @earth+@fire+@wind+@water<=7
          puts @earth.to_s+":"+@fire.to_s+":"+@wind.to_s+":"+@water.to_s
          key=(charid*8**4)+(@earth.to_i*8**3)+(@fire.to_i*8**2)+(@wind.to_i*8)+(@water.to_i)
          @text.text="KEY:"+key.to_s+"/tClass:"+Gsclass.find(Classkey.find_by_key(key).gsclass_id).name
    else
        count8(charid)
    end
  end
  
  def create
    super
    @splitter.setSplit(0, app.reg.readIntEntry("Settings", "splitSize"))
    show(PLACEMENT_SCREEN)

  end

end

if __FILE__ == $0
  FXApp.new do |app|
    CharacterKeyGen.new(app)
    app.create
    app.run
  end
end
